#include "main.h"

#define STRUKTURES 5

#define EIYLI if (y >= 0 && x >= 0 && y < S_HEIGHT && x < S_WIDTH)
#define EIYLI2 if (y1 >= 0 && x1 >= 0 && y1 < S_HEIGHT && x1 < S_WIDTH)


#define PICS 1

#define MPITUUS 35000.0

char kuvat[PICS][20] = {"data/Devil-goat.png"};

#define PITUUS 5
short int viiva[(PITUUS+1)*2] = {
	0, 0,
	50, -100,
	100, -50, 
 	200, 10,
  	300, 70,
	400, -30 };

#define LINE_AMOUNT 10

class kommando
{
	private:		
		
		SDL_Rect dst;
		SDL_Rect src;
		
		SDL_Surface *pics[PICS];
		
		Mix_Chunk *music;
		
		int x1, x2;
		int y1, y2;
		
		int modifier;
		
		int now1;
		
		void lataa_kuvat();
		
		void razer();

		void draw_line(int startx, int starty, int endx, int endy);
		
		int vari;
		
		int monestiko;
		
		void rotator(SDL_Surface *surface, float rot);
		
	public:
		int beginning;
		int now;
		void run();
		void audio();
		
		kommando();
};

void kommando::razer()
{
	int ykerroin = 100;
	
	int yseuraava[2];
	
	int xseuraava[2];
	
	for (int i = 0; i < PITUUS; i++)
	{
		if (now1/3 <= 400)
		{
			xseuraava[0] = viiva[(i*2)];
			xseuraava[1] = viiva[(i*2)+2];
			
			yseuraava[0] = viiva[(i*2)+1]+ykerroin;
			yseuraava[1] = viiva[(i*2)+3]+ykerroin;
		}
		else
		{
			xseuraava[0] = S_WIDTH - viiva[((i)*2)+2];
			xseuraava[1] = S_WIDTH - viiva[((i)*2)];
			
			yseuraava[0] = viiva[((i)*2)+3]+ykerroin;
			yseuraava[1] = viiva[((i)*2)+1]+ykerroin;
		}
		
		
		for (int j = -18; j < 19; j++)
		{
			if (j < 0)
				vari = ((255/2)*(3/(float(-j)))) + ((int)((255/8)*(3/(float(-j))))<<8) + ((255/100)<<16);
			else
				vari = ((255/2)*(3/(float(j)))) + ((int)((255/8)*(3/(float(j))))<<8) + ((255/100)<<16);
			draw_line(xseuraava[0],
					  yseuraava[0]+j,
				      xseuraava[1],
					  yseuraava[1]+j);
			if (j == 0)
				j++;
		}
		vari = 255 + ((255/8)<<8) + ((255/8)<<16);
		draw_line(xseuraava[0],
					  yseuraava[0],
				      xseuraava[1],
					  yseuraava[1]);
	}
}

void kommando::rotator(SDL_Surface *surface, float rot)
{
	Uint8 r, g, b;
	
	float cosine = cos(rot);
	
	for (int y = 0; y < surface->h; y++)
	{
		Uint32 *p = ((Uint32*)surface->pixels) + ((surface->pitch * y) >> 2);
		
		for (int x = 0; x < surface->w; x++)
		{
			

				int x1 = x+((( ( y - surface->h/2 ) * cosine ))) + 300;
 				int y1 = y-((( ( x - surface->w/2 ) * cosine )));

			
			SDL_GetRGB(*p, surface->format, &r, &g, &b);
			
			EIYLI2
				*(Uint32 *)((Uint8 *)screen->pixels + (y1) * screen->pitch + ((x1) << 2)) += SDL_MapRGB(screen->format, r, g, b);
			
			p++;
		}
	}
}

kommando::kommando()
{
	monestiko = 0;
	lataa_kuvat();

	music = Mix_LoadWAV("data/glory.ogg");
	Mix_Volume(Mix_PlayChannel(-1, music, 0),MIX_MAX_VOLUME/2);
}

void kommando::run()
{	
	now = SDL_GetTicks() - beginning;
	if (now - monestiko > 1000)
	{
		SDL_FillRect(screen, NULL, 0);
		monestiko = now;
	}	
 	
// 	SDL_BlitSurface(pics[0], NULL, screen, NULL);
	
	rotator(pics[0],(now/100)/7.);
	now1 = now%2000;
	razer();
	
	
}

void kommando::lataa_kuvat()
{
	for (int i = 0; i < PICS; i++)
		pics[i] = SDL_DisplayFormat(IMG_Load(kuvat[i]));
	

}


void kommando::audio()
{	
	beginning = SDL_GetTicks();
	now = SDL_GetTicks() - beginning;
//  	Mix_Volume(Mix_PlayChannel(-1, music[0], 0),MIX_MAX_VOLUME/5);
}

void kommando::draw_line(int startx, int starty, int endx, int endy) 
{

	int x0 = starty;
	 int x1 = endy;
	 int y0 = startx;
	 int y1 = endx;
	
	int steep;
	if (abs(y1 - y0) > abs(x1 - x0))
		steep = 1;
	else
		steep = 0;
     if (steep)
	 {
        int tep = x0;
		x0 = y0;
		y0 = tep;

		tep = x1;
		x1 = y1;
		y1 = tep;
	 }
     if (x0 > x1)
	 {
		int tep = x0;
		x0 = x1;
		x1 = tep;

		tep = y0;
		y0 = y1;
		y1 = tep;
	 }
     int deltax = x1 - x0;
     int deltay = abs(y1 - y0);
     int error = deltax / 2;
     int ystep;
     int y = y0;
     if (y0 < y1)
	 	ystep = 1;
	 else 
		 ystep = -1;
	 
	 SDL_LockSurface(screen);
     for (int x = x0; x < x1; x++)
	 {

		 if (now1/3 <=400)
         if (!steep)
		 {
			 if (y < S_WIDTH && y > 0 && x <  S_HEIGHT && x > 0 && y < now1/3)
		 		*(Uint32 *)((Uint8 *)screen->pixels + x * screen->pitch + (y<< 2)) += vari;
			 
		 }
		else
		{
			if (y < S_HEIGHT && y > 0 && x < S_WIDTH && x > 0 && x < now1/3)
				*(Uint32 *)((Uint8 *)screen->pixels + y * screen->pitch + (x<< 2)) += vari;
		}
		else if (now1/3 <= 820)
			         if (!steep)
		 {
			 if (y < S_WIDTH && y > 0 && x <  S_HEIGHT && x > 0 && y > (S_WIDTH - now1/3)+400)
		 		*(Uint32 *)((Uint8 *)screen->pixels + x * screen->pitch + (y<< 2)) += vari;
			 
		 }
		else
		{
			if (y < S_HEIGHT && y > 0 && x < S_WIDTH && x > 0 && x > (S_WIDTH - now1/3)+400)
				*(Uint32 *)((Uint8 *)screen->pixels + y * screen->pitch + (x<< 2)) += vari;
		}
         error = error - deltay;
         if (error < 0)
		 {
             y = y + ystep;
             error = error + deltax;
		 }
	 }
	 SDL_UnlockSurface(screen);
}
